JavaScript String match() Method |
您所在的位置:网站首页 › javascript matches › JavaScript String match() Method |
JavaScript String match() Method: Get Matchings From A String
In this tutorial, you will learn how to match a part of a string and get the matchings from it using regular expressions. We will discuss the JavaScript string match() method. ![]() The match() method in JavaScript matches a series of characters from a string using a regular expression. The method accepts a regular expression to find any match. If the match is found then it returns the matching content from the string as an array of strings. If nothing matches then it returns null. Example var str = "Learning To Code Is Learning To Create And Innovate"; var exp = /[A-Z]/g; // Matching capital characters // get matches var matches = str.match(exp); console.log(matches); // output: [ "L", "T", "C", "I", "L", "T", "C", "A", "I" ] ▶ Try ItThe above example uses /[A-Z]/g as a regular expression that matches all the capital letters from the string and returns as an array. Syntax Of match() methodThe syntax of the string match() method is following: string.match(regex); string - It is the string from which the match is to be found. regexp - It is the regular expression to match the string. If the given regexp is not a regular expression then the method implicitly converts it to regex by using the new RegExp(regexp).Let's see a few examples: Example 1: non-regex Example var str = "learning to code is learning to create and innovate"; // not a regular expression var matches = str.match("to"); console.log(matches); ▶ Try ItIn the above example, the passed argument in the match method is not a regular expression so the method implicitly converted it to regex and matched the content. Example 2: regex Example var str = "learning to code is learning to create and innovate"; // regular expression var matches = str.match(/to/g); console.log(matches);The above example uses /to/g as a regular expression which matches all the to from the string and returns as an array. Example 3: Nothing passed in the match method If regexp is not passed then it returns an array with 1 empty string. i.e [''] Example: no regexp const str = "learning to code is learning to create and innovate"; const matches = str.match(); // no regexp passed console.log(matches); Return from match() methodThe method returns an array of matching content that depends on the presence and absence of global flag (g) in the given regexp and matching contents. Which creates 3 different modes: regex doesn't have g flag:- If the g flag is not used in regex then the method returns only the first complete match and its capturing group with the index (position of the match) and input (input string). Example: global flag (g) const str = "learning to code is learning to create and innovate"; // without g flag const regexp = /l(earn)ing/; const matches = str.match(regexp); console.log(matches); // [ "learning", "earn" ] // Additional information console.log(matches.index); console.log(matches.input); ▶ Try Itregex have g flag:- If the g flag is used in the regex then it returns all the matching within the string without capturing groups and other details, that's why we need the matchAll method. Example: global flag (g) const str = "learning to code is learning to create and innovate"; // with g flag const regexp = /l(earn)ing/g; const matches = str.match(regexp); console.log(matches); // [ "learning", "learning" ] ▶ Try ItNothing matches:- If nothing matches in the string then it always returns null. Example: Nothing matches const str = "learning to code is learning to create and innovate"; const regexp = /[0-9]/g; // matching numbers const matches = str.match(regexp); console.log(matches); // null Case sensitivityOne thing to note about regex is that it is case-sensitive. For example, if you want to match 'to' in an example then it will find for only 'to' not 'To', 'tO' or 'TO'. Example: case sensitive const str = "To do or not to do."; const regexp = /to/g; console.log(str.match(regexp));If you want to get all occurrences of characters or substring regardless of their case then use flag i with regular expression. Example: using flag i const str = "To do or not to do."; const regexp = /to/gi; console.log(str.match(regexp)); Using match String In JavaScript # Example 1: Matching all integersIn the following example use the match() function to get all the integers from 0-9. Match all integers const str = "Adding 15 an 25 gives 40."; const regexp = /[0-9]/g; // matching numbers const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 2: Ignoring casesIn the following example find the character from the string by ignoring its cases. Use flag i to ignore cases in regular expression (both upper and lower case selection). Ignore cases const str = "ABWTCDXGOHI12355abcvfgthi"; // matching all alphabets const regexp = /[A-Z]/gi; const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 3: Matching a patternThe following example finds all 'example' followed by 1 or more numeric characters followed by decimals. Like 'example 1.1.1' to 'Example 2.5' Match all 'example' const str = "Jump from Example 2.3.5 to example 5.5.1"; // matching all examples const regexp = /(example) \d[(.)\d]*/gi; const matches = str.match(regexp); console.log(matches); ▶ Try It # Example 4: Random searchThe following example contains random non-RegExp objects as a parameter to the match() method. const str = "A random line with Infinity to Infinity and 100, NaN and null too."; console.log(str.match("random")); // ["random"] console.log(str.match(NaN)); // ["NaN"] console.log(str.match(Infinity)); // ["Infinity"] console.log(str.match(+Infinity)); // ["Infinity"] console.log(str.match(100)); // ["100"] console.log(str.match(+100)); // ["100"] console.log(str.match(null)); // ["null"] ▶ Try It![]() The additional properties are: index: It is the index at which match was found input: It is the search string method is applied upon. groups: It is capturing group whose keys and value are the names and capturing group respectively.![]() You can use the match() method if you want to get a string or a set of similar strings present in a string. You can use regular expressions or simple strings to match a string. It returns an array of matching elements if no element is matched then returns null. |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |